<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


include_once("util/properties.php");
include_once("util/dom.php");
include_once("script/script_handler.php");
include_once("util/dirutils.php");
include_once("util/generate_html_util.php");

if(!isset($semf_root) )
{
	$semf_root = '../';
	$page_xml = $_GET['page_xml'];
	$result_dir = $_GET['result_dir'];
	$project_home = $_GET['project_home'];
	
	// Define that we are returning XML content & not to cache:
	header('Content-Type: text/xml');
	header('Cache-control: no-cache');
}

if ( file_exists($page_xml))
{
	// load the xml
	$doc = new DOMDocument();
	$doc->load($page_xml);
	$doc->validate();
	
	$container = NULL;
	$component = NULL;
	$component_name = NULL;
	
	$container = $doc->documentElement;
	
	//create a script parser..
	$script_handler = new script_handler($semf_root, $project_home);
	
	//some important properties..
	$html_properties = array();
	
	$generate_result = generate_component($container, $doc, $script_handler, $html_properties, $semf_root);
	
	$html_properties = $generate_result[1];
	$result = $generate_result[0];
	
	$content = "";
	$script_text = "";
	
	if( $result != NULL )
	{
		$script_text = extract_scripts($result->documentElement, NULL);
		$content = serialize_node($result->documentElement);
		//$result->saveXML($result->documentElement);
	}
	//$title = $html_properties['title'];
	$page_title = $html_properties['title'];
	$base_page_xml = basename($page_xml);

	$pos = strpos($base_page_xml, ".xml");
	$page_name = substr($base_page_xml, 0, $pos);
	$title = $page_name; //patch to page name..
	
	$result_html = $result_dir.$title."_html.php";
	$result_php_name = $title.".php";
	$result_js_name = $title.".js";
	$result_decl_js_name = $title."_decl.js";
	
	$result_php_file = $result_dir.$result_php_name;
	$result_js_file = $result_dir.$result_js_name;
	$result_decl_js_file = $result_dir.$result_decl_js_name;
	
	echo "<message><error>";
	$script_info = 
		$script_handler-> generate_script($result_dir, $result_php_file,
									 $result_js_file, $result_decl_js_file );
	echo "</error></message>";
	
	//echo $result_html;
	
	//copy store..
	 copy_dir($semf_root."store/", $result_dir."../", "store");
	
	//print_r($html_properties);
	$js_includes = $script_info["js"];
	$css_includes = $script_info["css"];
	
	$html_file_content = 
	"<?php
		\$intergrated = true;
		header('Cache-control: no-cache');
		include_once(\"${result_php_name}\");
	?>
	<html>
		<head>
		
			
		<title>$page_title</title>
		
		<script src=\"${result_js_name}\"></script>
		<script src=\"${result_decl_js_name}\"></script>
		${js_includes}
		${css_includes}
			
		</head>
		<body>
			<?php
				echo \$php_return_data;
			?>
			
			$content
			
			<script language=\"javascript\">

				$script_text
				SEMF[\"dataModelFileName\"] = \"{$result_php_name}\";
				SEMF.init();
			</script>
		</body>
	</html>
	";

	$result_html_file = fopen( $result_html, "w+");
	fwrite($result_html_file, $html_file_content, strlen($html_file_content));
	fclose($result_html_file);
	//echo $html_file_content;
}


?>
